#include "../source/Server.hpp"
void HandleClose(Channel* channel)
{
    std::cout << "close" << channel->Fd() << std::endl;
    channel->Remove();//移除监控
    delete channel;

}
void HandleRead(Channel* channel)
{
    int fd = channel->Fd();
    char buf[1024] = {0};
    int ret = recv(fd, buf, 1023, 0);
    if(ret <= 0)
    {
        return HandleClose(channel);//关闭释放
    }
    std::cout << buf << std::endl;
    channel->EnableWrite();//启动可写事件
}
void HandleWrite(Channel* channel)
{
    int fd = channel->Fd();
    const char* data = "回复客户端的消息!!";
    int ret = send(fd, data, strlen(data), 0);
    if(ret < 0)
    {
        return HandleClose(channel);//关闭释放
    }
    channel->DisableWrite();//关闭写监控
}
void HandleError(Channel* channel)
{
    return HandleClose(channel);//关闭释放
}
void HandleEvent(Channel* channel)
{
    std::cout<<"有了一个事件!!\n";
}
void Accepter(Poller* poller, Channel* lst_channel)
{
    int fd = lst_channel->Fd();
    int newfd = accept(fd, NULL, NULL);
    if (newfd < 0)
    {
        return;
    }
    Channel* channel = new Channel(poller, newfd);
    channel->setReadCallback(std::bind(HandleRead, channel)); //为通信套接字设置可读事件的回调函数
    channel->setWriteCallback(std::bind(HandleWrite, channel));//可写事件的回调函数
    channel->setCloseCallback(std::bind(HandleClose, channel));//关闭事件的回调函数
    channel->setErrorCallback(std::bind(HandleError, channel));//错误事件的回调函数
    channel->setEventCallback(std::bind(HandleEvent, channel));//任意事件的回调函数
    channel->EnableRead();
}
int main()
{
    Poller poller;
    Socket lst_sock;
    bool ret = lst_sock.CreateServer(8088);
    //为监听套接字，创建一个Channel进行事件的管理，以及事件的处理
    Channel channel(&poller, lst_sock.GetFd());
    //回调中，获取新连接，为新连接创建channel并且添加监控
    channel.setReadCallback(std::bind(Accepter, &poller, &channel));
    channel.EnableRead();//启动可读事件监控
    while(1)
    {
        std::vector<Channel*> actives;
        poller.Poll(&actives);
        for(auto &a : actives)
        {
            a->HandlerEvent();
        }
    }
    lst_sock.Close();
    return 0;
}